package com.houtai.educms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.houtai.commonutils.Result;
import com.houtai.educms.entity.CrmBanner;
import com.houtai.educms.entity.vo.BannerQuery;
import com.houtai.educms.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author xst
 * @since 2022-11-28
 */
@RestController
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    //1.逻辑删除
    @ApiOperation(value = "逻辑删除banner")
    @DeleteMapping("{id}")
    public Result removeBanner(@ApiParam(name="id",value = "banner的ID",required = true) @PathVariable int id){
        boolean flag=bannerService.removeById(id);
        if(flag){
            return Result.ok();
        }
        else {
            return Result.error().message("删除失败");
        }

    }
    //2.多条件组合查询带分页
    @ApiOperation(value = "多条件组合查询带分页")
    @PostMapping("pageBannerCondition/{current}/{limit}")
    public Result pageBannerCondition(@PathVariable long current,
                                      @PathVariable long limit,
                                      @RequestBody(required = false) BannerQuery bannerQuery){
        //创建page对象
        Page<CrmBanner> pageT = new Page<>(current,limit);
        //调用方法实现分页
        //构建条件
        QueryWrapper<CrmBanner> wrapper=new QueryWrapper<>();
        //wrapper
        //动态sql
        String title = bannerQuery.getTitle();
        String begin = bannerQuery.getBegin();
        String end = bannerQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(title)){
            //构建条件
            wrapper.like("title",title);

        }
        if(!StringUtils.isEmpty(begin)){
            //构建条件
            wrapper.gt("gmt_create",begin);

        }
        if(!StringUtils.isEmpty(end)){
            //构建条件
            wrapper.le("gmt_create",end);

        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        bannerService.page(pageT,wrapper);

        long total = pageT.getTotal();//总记录数
        List<CrmBanner> records = pageT.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);

    }
    //3.添加banner
    @ApiOperation(value = "添加banner")
    @PostMapping("addBanner")
    public Result addBanner(@RequestBody @Valid  CrmBanner eduBanner){
        boolean flag=bannerService.save(eduBanner);
        if(flag){
            return Result.ok();
        }
        else {
            return Result.error().message("添加失败");
        }
    }
    //6.根据讲师id进行查询
    @ApiOperation(value = "根据id进行查询")
    @GetMapping("getBannerId/{id}")
    public Result getBannerId(@PathVariable Integer id){
        CrmBanner eduBanner=bannerService.getById(id);
        return Result.ok().data("banner",eduBanner);

    }
    //7.修改
    @ApiOperation(value = "修改")
    @PostMapping("updateBanner")
    public Result updateBanner(@RequestBody @Valid CrmBanner eduBanner){
        boolean f=bannerService.updateById(eduBanner);
        if(f){
            return Result.ok();
        }
        else {
            return Result.error().message("修改失败");
        }

    }

}
